<template>
  <div class='m-sys_balanceOfProject'>
    <div style='padding-bottom: 20px;'>
      <el-button type='primary' @click='add'>添加项目</el-button>
    </div>
    <a-table :data='data' :cols='tableKeys'>
      <el-table-column align="center" label="操作" width='260' slot='ahead'>
        <template slot-scope="scope">
          <div>
            <el-button size='mini' @click='toAttr(scope.row.id)'>属性</el-button>
            <el-button size='mini' @click='toGoods(scope.row.id)'>商品</el-button>
            <el-button size='mini' @click='set(scope.row)'>修改</el-button>
            <el-button size='mini' @click='deleteObj(scope.row.id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>

    <add-dialog ref='addDialog' @update='search' />
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import addDialog from './components/addDialog'
import { costFdType } from '@/api/costFdType'

export default {
  name: 'sys_balanceOfProject',

  components: {
    addDialog
  },

  data() {
    const filterType = (value) => {
      const obj = costFdType.filter(v => v.value === value)[0]
      return obj ? obj.label : ''
    }
    return {
      tableKeys: [
        { prop: 'type', label: '收支类别', minWidth: '100', filter: filterType },
        { prop: 'name', label: '项目名称', minWidth: '100' }
      ],
      data: [],
      ifRequesting: false
    }
  },

  mounted() {
    this.search()
  },

  activated() {
    this.search()
  },

  methods: {
    ...mapMutations(['setTabRoutes']),

    routerPush(type, args) {
      // push 到缓存里
      this.setTabRoutes({ type: 'add', obj: { ...this.getRouterInfoAccordingName(type), ...args } })
    },

    search() {
      this.$refs.page.changePage()
    },

    async fetchData(aPageNum, aPageSize) {
      return this.$api.inventGetCosts({
        aFirmId: this.aFirmId,
        aPageNum,
        aPageSize
      })
    },

    getData(data) {
      this.data = data
    },

    add() {
      this.$refs.addDialog.$show()
    },

    set(data) {
      this.$refs.addDialog.$setData(JSON.parse(JSON.stringify(data)))
    },

    toAttr(id) {
      const query = { id }
      this.routerPush('sys_balanceOfProject_attr', { query })
      this.$router.push({ name: 'sys_balanceOfProject_attr', query })
    },

    toGoods(id) {
      const query = { id }
      this.routerPush('sys_balanceOfProject_goods', { query })
      this.$router.push({ name: 'sys_balanceOfProject_goods', query })
    },

    deleteObj(id) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该收支吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.inventDeleteCost({ aId: id })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    }
  }
}
</script>

<style lang='less'>

</style>
